/*日期格式化*/
function dateFormat (date, format='yyyy-MM-dd hh:mm') {
    date = new Date(date);
    var map = {
        "M": date.getMonth() + 1, //月份
        "d": date.getDate(), //日
        "h": date.getHours(), //小时
        "m": date.getMinutes(), //分
        "s": date.getSeconds(), //秒
        "q": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
    };
    format = format.replace(/([yMdhmsqS])+/g, function(all, t){
        var v = map[t];
        if(v !== undefined){
            if(all.length > 1){
                v = '0' + v;
                v = v.substr(v.length-2);
            }
            return v;
        }
        else if(t === 'y'){
            return (date.getFullYear() + '').substr(4 - all.length);
        }
        return all;
    });
    return format;
}
/*分钟数转时间*/
function totime(time=0) {
    var date = time+new Date('1970-12-30 00:00:00').getTime();
    return dateFormat(date,'hh:mm');
}
/*获取时间戳*/
function getTimestamp(time=0) {
    var ymd = '1970-12-30';
    if(time < 0){
        ymd = '1970-12-31';
    }
   return time+new Date(ymd+' 00:00:00').getTime();
}

/*返回html中文字的部分内容*/
exports.toDescription=function (content,num = 220) {
    var res = content.replace(/<[^>]+>/g,"");
    return res.substr(0,num);
};
exports.toJson = function (data) {
   if(data){
       return JSON.parse(data);
   }
    return {};
};
/*日期格式化*/
exports.dateFormat = dateFormat;
/*时间格式化*/
exports.totime = totime;
/*图片路径标准化*/
exports.imgPath = (data,type = 1)=>{
    if(!data){
        return  '../../../public/images/logoTag.jpg';
    }
    if(data.toString().substr(0,4)=='http'){
        return data;
    }
    if(type === 1){
       return 'https://res.bcdaren.com/'+data;
       // return 'http://res.im.heiwuxs.cn'+'/'+data;
    }
    if(type === 2){
        return 'https://api.bcdaren.com/'+data;
       // return 'http://api.im.heiwuxs.cn'+'/'+data;
    }
    if(type === 3){
        var path = data.split(' ')[0] || data;
        return 'https://res.bcdaren.com/'+path;
        // return 'http://api.im.heiwuxs.cn'+'/'+data;
    }
};
exports.projectStatus = function (status) {
    var projectStatus = ['待审核','待接单','审核被拒','项目进行中','项目已完成','撤单中','撤单成功'];
    return projectStatus[status -1]
};
/*是否是直播中*/
exports.isBroadcast = function (item) {
    const start = getTimestamp(item.beginTime);
    const end = getTimestamp(item.endTime);
    let current = (new Date('1970-12-30 '+dateFormat(new Date().getTime(),'hh:mm:ss'))).getTime();
    const flag = current >= start && current <= end;
    return flag&&item.openIs;
};
/*是否开班中*/
exports.showIs = function (item) {
    let current = new Date().getTime();
    const start = new Date(item.beginDate);
    const end = new Date(item.endDate);
    return current>=start && current<=end;
};
/*获取不同图片*/
exports.imgArrarys = function (data) {
    if(!data){
        return [];
    }
    return data.split(' ');
};
/*获取不同时间显示*/
exports.toTimeStr = function (dateTimeStamp) {
    var minute = 1000 * 60;      // 把分，时，天，周，半个月，一个月用毫秒表示
    var hour = minute * 60;
    var day = hour * 24;
    var week = day * 7;
    var halfamonth = day * 15;
    var month = day * 30;
    var year = month * 12;
    var now = new Date().getTime();   // 获取当前时间毫秒
    var result = '刚刚';
    var diffValue = now - dateTimeStamp;// 时间差
    if(diffValue < 0){
        return '';
    }
    var minC = diffValue/minute;  // 计算时间差的分，时，天，周，月
    var hourC = diffValue/hour;
    var dayC = diffValue/day;
    var weekC = diffValue/week;
    var monthC = diffValue/month;
    var yearC = diffValue/year;
    if(yearC>1){
        return dateFormat(dateTimeStamp);
    }
    if(monthC >= 1 && monthC <= 12){
        result = " " + parseInt(monthC) + " 月前"
    }else if(weekC >= 1 && weekC <= 4){
        result = " " + parseInt(weekC) + " 周前"
    }else if(dayC >= 1 && dayC <= 7){
        result = " " + parseInt(dayC) + " 天前"
    }else if(hourC >= 1 && hourC <= 24){
        result = " " + parseInt(hourC) + " 小时前"
    }else if(minC >= 1 && minC <= 59.9){
        result =" " + parseInt(minC) + " 分钟前"
    }else if(diffValue >= 0 && diffValue <= minute){
        result = "刚刚"
    }else {
        return dateFormat(dateTimeStamp);
    }
    return result;
};
/*小数位数保留*/
exports.toFloat = function (data,format) {
    if(format==0){
        return  Math.floor(data/1);
    }
    return (data/1).toFixed(format);
};
/*首页分类*/
exports.subscribeType = function (type) {
    var arr = ['视频','线上班','公告','问答','项目','源码','博客'];
    return (arr[type-1]);
};
/*首页分类*/
exports.subscribeUrl = function (type) {
    var arr = ['video_play.html','online_content.html','notice_content.html','answer_content.html','project_content.html','source_content.html','blog_content.html'];
    return (arr[type-1]);
};
exports.projectPayType = function (type) {
    var payType = ['全款','定金'];
    return payType[type-1];
};

exports.supportPays = function (type) {
    var json = {
        'rmb':'¥',
        'gold':'金币',
        'rmb,gold':'人民币/金币',
    };
    return json[type];
};
/*小数位数保留*/
exports.codeContent = function (data) {
    var reg = /classex="plain"|classex="c\+\+"|classex="c#"|classex="java"|classex="js"|classex="php"|classex="python"|classex="sql"/g;  //  找到北京 或者天安门 全局匹配
    return  data.replace(reg,function (res) {
        var result = res.replace(/classex="/g,'');
        return 'class="brush:'+result;
    });
};